package com.fanix5.javamvvm.ui.home;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;


import android.view.View;


import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.fanix5.javamvvm.BR;
import com.fanix5.javamvvm.Ext.CustomViewExt;
import com.fanix5.javamvvm.R;
import com.fanix5.javamvvm.adapter.DoctorListAdapter;
import com.fanix5.javamvvm.base.MvvmBaseActivity;
import com.fanix5.javamvvm.bean.DoctorBean;
import com.fanix5.javamvvm.bean.ListDataUiState;
import com.fanix5.javamvvm.databinding.ActivityDoctorListBinding;
import com.fanix5.javamvvm.utils.ToastUtil;
import com.fanix5.javamvvm.viewmodel.state.DoctorListViewModel;
import com.fanix5.javamvvm.widget.DefineLoadMoreView;
import com.fanix5.javamvvm.widget.RecyclerViewHelper;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;
import com.orhanobut.logger.Logger;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

import java.util.List;

public class DoctorListActivity extends MvvmBaseActivity<ActivityDoctorListBinding, DoctorListViewModel> {

    private DoctorListAdapter mAdapter;

    private LoadService loadService;

    private RecyclerViewHelper mRecyclerViewHelper;

    @Override
    protected int initVariableId() {
        return BR.viewModel;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_doctor_list;
    }

    @Override
    protected void initView() {

        setToolBar(this,mDatabind.includeToolBar.toolBar, false,"医生列表");


        mAdapter = new DoctorListAdapter();



        DefineLoadMoreView footView = new DefineLoadMoreView(this);
        SwipeRecyclerView.LoadMoreListener loadMoreListener = () -> {
            footView.onLoading();
            mViewModel.getData(false);
        };

        CustomViewExt.getInstance().initSwipeRecyclerView(this, mDatabind.recyclerView,mAdapter,footView,loadMoreListener);
        CustomViewExt.getInstance().initSwipeRefreshLayout(mDatabind.swipeRefreshLayout);

        loadService = LoadSir.getDefault().register(mDatabind.swipeRefreshLayout, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {

                requestData(true);
            }
        });

    }


    @Override
    protected void initViewObservable() {

        mViewModel.liveData.observe(this, new Observer<ListDataUiState<List<DoctorBean>>>() {
            @Override
            public void onChanged(ListDataUiState<List<DoctorBean>> listListDataUiState) {
                Logger.e("doctorBeans:" + listListDataUiState);
                CustomViewExt.getInstance().loadListData(listListDataUiState, mDatabind.recyclerView, mDatabind.swipeRefreshLayout, mAdapter,loadService);
            }
        });

    }

    @Override
    protected void initListener() {
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                String name = mAdapter.getData().get(position).getName();
                ToastUtil.getInstance().show(name);
            }
        });

        mDatabind.swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                requestData(true);
                mRecyclerViewHelper = new RecyclerViewHelper(mDatabind.recyclerView, 15);
                mRecyclerViewHelper.setListener(() -> {
                    mViewModel.getData(false);
                });
            }
        });

    }

    @Override
    public void onReturn() {
        super.onReturn();
        ToastUtil.getInstance().show("哈哈哈哈");
    }

    private void requestData(boolean isRefresh){
        CustomViewExt.getInstance().showLoad(loadService);
        mViewModel.getData(isRefresh);
    }

    @Override
    protected void initData() {
        requestData(true);
    }
}